package cn.psvmc.cxzapi.service.system;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.enums.ResultCode;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.system.*;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.system.TRole;
import cn.psvmc.cxzapi.model.system.TRoleMenu;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色表 服务实现类
 * @Author auto
 * @Date 2024-08-08
 */
@Service
public class TRoleService extends AbstractService<TRoleMapper, TRole> {

    @Resource
    private TRoleMapper tRoleMapper;
    @Resource
    private TRoleMenuMapper tRoleMenuMapper;
    @Resource
    private TRoleMenuService tRoleMenuService;
    @Resource
    private ServiceUtil serviceUtil;
    @Resource
    private TMenuMapper tMenuMapper;

    /**
    * @Date auto 2024-08-08 新增角色表
    * @Param tRole 角色表
    * @Return int
    */
    @Transactional(rollbackFor = Exception.class)
    public int add(TRole tRole) {
        serviceUtil.setStoreId(tRole);
        if (tRole.getStoreid() != null && tRole.getStoreid() == 0) {
            tRole.setStoreid(null);
        }
        checkRoleName(tRole.getRolename(), tRole.getStoreid());//角色名称是否存在
        int insert = tRoleMapper.insert(tRole);
        saveRoleMenu(tRole);//保存角色权限
        return insert;
    }

    //角色名称是否存在
    private void checkRoleName(String rolename, Integer storeid) {
        Integer count = tRoleMapper.countByRoleNameAndStoreid(rolename, storeid);
        if (count > 0) {
            throw new ServiceException("角色名称已存在！");
        }
    }

    //保存角色权限
    private void saveRoleMenu(TRole tRole) {
        Integer roleid = tRole.getRoleid();
        List<TRoleMenu> menus = tRole.getMenus();
        if (ToolUtils.isNotEmpty(menus)) {
            menus.forEach(m -> m.setRoleid(roleid));
            tRoleMenuService.saveBatch(menus);
        }
    }

    /**
    * @Date auto 2024-08-08 更新角色表
    * @Param tRole 角色表模型
    * @Return int
    */
    @Transactional(rollbackFor = Exception.class)
    public int edit(TRole tRole){
        // 通过id查询数据处理 详情
        TRole originTRole = tRoleMapper.selectById(tRole.getRoleid());
        if(originTRole == null){
            throw new ServiceException("未查询到对应的角色表信息！");
        }

        // 比较数据版本号
        if(!originTRole.getRversion().equals(tRole.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }
        String rolename = tRole.getRolename();
        if(rolename!= null &&!rolename.equals(originTRole.getRolename())) {
            checkRoleName(rolename, originTRole.getStoreid());//角色名称是否存在
        }
        tRole.setRoletype(null);
        List<TRoleMenu> menus = tRole.getMenus();
        if (ToolUtils.isListNotEmpty(menus)) {
            tRoleMenuMapper.deleteByRoleId(tRole.getRoleid());//删除角色权限
            saveRoleMenu(tRole);//保存角色权限
        }

        // 字段为null 不更新
        tRole.setStoreid(null);
        return tRoleMapper.updateById(tRole);
    }

    /**
    * @Date auto 2024-08-08 查询角色表详情
    * @Param id 唯一id
    * @Return cn.psvmc.cxzapi.model.system.TRole
    */
    public TRole detail(Integer id) {
        TRole tRole = tRoleMapper.selectById(id);
        tRole.setMenus(tRoleMenuMapper.listMenuIdByRoleId(id));//获取角色权限ids
        return tRole;
    }

    /**
    * @Date auto 2024-08-08 删除角色表
    * @Param id 唯一id
    * @Param rversion 版本号
    * @Return int
    */
    public int delete(Integer id, Integer rversion){
        // 查询角色表详情
        TRole tRole = tRoleMapper.selectById(id);
        if(tRole == null){
            throw new ServiceException("未查询到对应的角色表信息！");
        }

        // 比较数据版本号
        if(!rversion.equals(tRole.getRversion())){
            throw new ServiceException("数据已更新，请刷新后重试！");
        }
        if (tRole.getRoletype().equals(1)) {
            throw new ServiceException(ResultCode.NO_POWER.getMsg());
        }

        return tRoleMapper.deleteById(id);
    }

    /**
     * @Description 查询角色表列表
     * @Date 2024/8/11 21:32
     * @Author YWX
     * @Param [tRole]
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public List<Map<String, Object>> listAll(SearchEntity entity){
        serviceUtil.setStoreId(entity);//设置商家id
        return tRoleMapper.listAll(entity);
    }

    /**
     * @Description 选择列表
     * @Date 2024/8/13 13:22
     * @Author YWX
     * @Param []
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listForSel(SearchEntity entity) {
        return tRoleMapper.listForSel(entity);
    }
}
